<?php
declare(strict_types=1);

namespace Modules\Warehouse\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Modules\Warehouse\Models\Storagelocation;
use Illuminate\Http\Request;
use extend\services\warehouse\{warehouseService};//引入自定义类
use Illuminate\Support\Facades\DB;
use Modules\Warehouse\Http\Repositories\StorageReget;

class StorageController extends Controller
{
    public function __construct(
        protected readonly Storagelocation $model
    ){}

    /**
     * @param Request $request
     * @return mixed
     */

     public function index(Request $request): mixed
     {

        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        //  $dataList = $this->model->getList();
        $dataList = StorageReget::listtable($request->all());
      
       
        foreach($dataList as $k=>$v){
        $dataList[$k]['fname'] = '';
            if(!empty($v['owning_id'])){
                $stash = DB::table('stash')->select('fmaterial','fnumber','fname','fuseorgid_number','fuseorgid_name')->where('id',$v['owning_id'])->first();
                if(!empty($stash)){
                    
                    $dataList[$k]['owning_id'] = $stash->fname;
                    $dataList[$k]['fuseorgid_number'] = $stash->fuseorgid_number;

                    $dataList[$k]['fuseorgid_name'] = $stash->fuseorgid_name;
                }
            }
        
        $dataList[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]:'';
        $dataList[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]:'';
            
        } 
        return $dataList;
   
     }

    // public function index(Request $request): mixed
    // {
       
    //     $res = $request->all();
    //     if(!isset($res['owning_id'])){
    //         $res = [];
    //     }

    //     $kingDeeService = new warehouseService();
    //     $data = $kingDeeService->warehousedataTwo($res);
    //     $datauser = $kingDeeService->uerdata();

    //     $resdata = [];
    //     if(empty($data)){
    //         return [];
    //     }
        
    //     foreach($data as $k=>$v){
    //         $resdata[$v['FSTOCKID']] = $v['FName']; 
    //     }
        
    //     $dataList = $this->model->getList();
      
    //     if(isset($res['owning_id'])){
    //         $dataListres = [];
    //         foreach($dataList as $k=>$v){
    //             if(isset($resdata[$v['owning_id']])){
    //                 $dataListres[$k]['owning_id'] = $resdata[$v['owning_id']];
    //                 $dataListres[$k]['created_at'] = $v['created_at'] ;
    //                 $dataListres[$k]['creator'] = $v['creator'];
    //                 $dataListres[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]['username']:'';
    //                 $dataListres[$k]['id'] = $v['id'];
    //                 $dataListres[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]['username']:'';
    //                 $dataListres[$k]['person'] = $v['person'];
    //                 $dataListres[$k]['remarks'] = $v['remarks'];
    //                 $dataListres[$k]['repositoryname'] = $v['repositoryname'];
    //                 $dataListres[$k]['updated_at'] = $v['updated_at'];
    //                 $dataListres[$k]['warehousecode'] = $v['warehousecode'];
    //             }
    //         } 
    //         return $dataListres;
        
    //     }else{
    //         foreach($dataList as $k=>$v){
              
    //             if(isset($resdata[$v['owning_id']])){
                    
    //                 $dataList[$k]['owning_id']  = $resdata[$v['owning_id']];
    //             }else{
    //                 $dataList[$k]['owning_id']  = '';
    //             }
    //             $dataList[$k]['creator_id'] = isset($datauser[$v['creator_id']])?$datauser[$v['creator_id']]:'';
    //             $dataList[$k]['modifier'] = isset($datauser[$v['modifier']])?$datauser[$v['modifier']]:'';
               
    //         }
            
          
    //         return $dataList;
    //     }
       
    // }

    /**
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request)
    {
        $data = $request->all();
        $stash = $this->model
        ->select('warehousecode')
        ->where('warehousecode',$data['warehousecode'])
        ->first();
    
        $owning_id= $this->model
        ->select('warehousecode','owning_id')
        ->where('warehousecode',$data['warehousecode'])
        ->where('owning_id',$data['owning_id'])
        ->first();
     

        if(!empty($stash)||!empty($owning_id)){
         throw new FailedException('仓库库位代号不能重复');
        }
        return $this->model->storeBy($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function show($id)
    {
        return $this->model->firstBy($id);
    }

    /**
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update($id, Request $request)
    {
        
        $data = $request->all();
        // $data['person'] = '22';//仓库人
        $data['modifier'] =$this->getLoginUserId();//当前登录用户名称
       
        return $this->model->updateBy($id, $data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function destroy($id)
    {
        return $this->model->deleteBy($id);
    }


     /**
     * 所属仓库接口
     * @return mixed
     */
    public function warehousedataApi()
    { 
     
        $data = DB::table('stash')->select('id','fmaterial','fnumber','fname','fuseorgid_number','fuseorgid_name')->get()->toArray();
        return ['code'=>1000,'message'=>'扫描成功','data'=>$data];
        // $kingDeeService = new warehouseService();
        // $warehousedataApi = $kingDeeService->warehousedataApi();
      
    }

    
     /**
     * 仓库负责人接口
     * @return mixed
     */
    public function uerdataApi()
    { 
        $kingDeeService = new warehouseService();
        $datauser = $kingDeeService->uerdata();
        return $datauser;
    }

   /**
     *库位打印接口
     */
    public function Libraryprint(Request $request)
    {   

        $data = $request->all();
        $kingDeeService = new warehouseService();

        $data = $this->model->select('warehousecode','repositoryname','owning_id')
            ->whereIn('id',$data['id'])
            ->get()
            ->toArray();
     
        foreach($data as $k=>$v){
                 $stash = DB::table('stash')->select('fmaterial','fnumber','fname','fuseorgid_number','fuseorgid_name')->where('id',$v['owning_id'])->first();
                 $data[$k]['fnumber'] = $stash->fnumber;
                 $data[$k]['fname'] = $stash->fname;
                 $data[$k]['fuseorgid_number'] = $stash->fuseorgid_number;
                $fnumber = $kingDeeService->createQrCode('100',$v['warehousecode'],$v['warehousecode']);
                $data[$k]['Twocode']  = env('APP_URL').'/uploads/barcode/'.$v['warehousecode'].'.png';
        }       
        return ['code'=>1000,'message'=>'添加成功','data'=>$data];
    }


    

   


}
